package com.example.myapplication.ui.theme;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.ui.theme.dao.CartDao;
import com.example.myapplication.ui.theme.dao.GoodsDao;
import com.example.myapplication.ui.theme.model.GoodsInfo;

import java.util.List;

@SuppressLint("SetTextI18n")
public class ShoppingChannelActivity extends AppCompatActivity {
    private TextView tv_count; // 声明一个文本视图对象
    private GridLayout gl_channel; // 声明一个商品频道的网格布局对象
    private CartDao cartDao; // 声明一个购物车的持久化对象
    private GoodsDao goodsDao; // 声明一个商品的持久化对象
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping_channel);
        TextView tv_title = findViewById(R.id.tv_title);
        tv_title.setText("手机商场");
        tv_count = findViewById(R.id.tv_count);
        gl_channel = findViewById(R.id.gl_channel);
        findViewById(R.id.iv_back).setOnClickListener(v -> finish());
        findViewById(R.id.iv_cart).setOnClickListener(v -> {
            // 从商场页面跳到购物车页面
            Intent intent = new Intent(this, ShoppingCartActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // 设置启动标志
            startActivity(intent); // 跳转到购物车页面
        });
        tv_count.setText("" + MainApplication.goodsCount);
        cartDao = MainApplication.getInstance().getCartDB().cartDao();
        goodsDao = MainApplication.getInstance().getGoodsDB().goodsDao();
    }

    @Override
    protected void onResume() {
        super.onResume();
        tv_count.setText("" + MainApplication.goodsCount);
        showGoods(); // 展示商品列表
    }

    private void showGoods() {
        int screenWidth = getResources().getDisplayMetrics().widthPixels; //获取屏幕宽度
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(screenWidth / 2, ViewGroup.LayoutParams.WRAP_CONTENT);
        gl_channel.removeAllViews();
        List<GoodsInfo> goodsInfos = goodsDao.queryAllGoods();
        for (GoodsInfo info : goodsInfos) {
            View view = LayoutInflater.from(this).inflate(R.layout.item_goods, null);
            ImageView iv_thumb = view.findViewById(R.id.iv_thumb);
            TextView tv_name = view.findViewById(R.id.tv_name);
            TextView tv_price = view.findViewById(R.id.tv_price);
            Button btn_add = view.findViewById(R.id.btn_add);
            tv_name.setText(info.getName());
            iv_thumb.setImageURI(Uri.parse(info.getPicPath()));
            tv_price.setText("" + (int) info.getPrice());
            iv_thumb.setOnClickListener(v -> {
                Intent intent = new Intent(this, ShoppingDetailActivity.class);
                intent.putExtra("goods_id", info.getId());
                startActivity(intent); // 跳到商品详情页面
            });
            btn_add.setOnClickListener(v -> addToCart(info.getId(), info.getName()));
            gl_channel.addView(view, params);
        }
    }

    private void addToCart(long id, String name) {
        MainApplication.goodsCount++;
        tv_count.setText("" + MainApplication.goodsCount);
        cartDao.save(id); // 把该商品填入购物车数据库
        Toast.makeText(this, "已添加" + name + "到购物车", Toast.LENGTH_SHORT).show();
    }
}